package com.ddm.ddm.controller;


import com.ddm.ddm.common.RestResponse;
import com.ddm.ddm.entity.Require;
import com.ddm.ddm.query.RequirePage;
import com.ddm.ddm.service.IRequireService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.time.LocalDate;
import java.util.List;
import javax.validation.constraints.NotBlank;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/require",name = "需求")
public class RequireController {

  @Autowired
  private IRequireService iRequireService;

  @PostMapping(value = "/list", name = "列表")
  public RestResponse getAllRequire(@RequestBody RequirePage requirePage) {
    PageHelper.startPage(requirePage.getPageStart(), requirePage.getPageSize());
    List<Require> sysUserList = iRequireService.getAll(requirePage);
    PageInfo pageInfo = new PageInfo(sysUserList);
    if (pageInfo != null) {
      return RestResponse.ok("查询成功", pageInfo);
    }
    return RestResponse.error("查询失败");
  }

  @PostMapping(value = "/save",name = "创建")
  public RestResponse saveRequire(@RequestBody Require require){
    require.setRstarttime(LocalDate.now());
    int i = iRequireService.add(require);
    if (i>0){
      return RestResponse.ok("创建成功！");
    }
    return RestResponse.error("创建失败");
  }

  @GetMapping(value = "/detail/{id}",name = "详情")
  public RestResponse getDetailById(@PathVariable("id") String id){
    Require byId = iRequireService.getDetailById(id);
    if (byId!=null){
      return RestResponse.ok("获取成功",byId);
    }
      return RestResponse.error("获取失败");
  }

  @PutMapping(value = "/upRequire",name = "修改")
  public RestResponse upRequire(@RequestBody Require require){
    require.setRstarttime(LocalDate.now());
    int i = iRequireService.UpRequire(require);
    if (i>0){
      return RestResponse.ok("修改成功！");
    }
    return RestResponse.error("修改失败");
  }


}
